<?php
function cache_get_user($u,$con=true){
	$c=new cache;
	if($con){
	$dt=$c->load($u,"user");
	}
	if(!$dt){
	 $db=new db;
	 $db->connect();
	 $db->query("select * from users where username='".$u."'");
	 $db->close();
	 
		if($db->num_rows()>0){
			$dt=$db->fetchOne();	
			$dt['url']=HOME."channel/".$dt['username'];
			if($dt['name']==''){
				$dt['show']=$dt['username'];
			}else{
				$dt['show']=$dt['name'];
			}
			
			 if($dt['avatar']!=""){
                 $ava=explode("_",$dt['avatar']);
 				 $dt['avatar1']=AVATAR."100/".$ava[0]."/".$ava['1']."/".$ava['2']."/".$dt['avatar'];
				 $dt['lavatar1']=AVATAR."100/".$ava[0]."/".$ava['1']."/".$ava['2']."/".$dt['avatar'];
 		   }else
		   if($dt['facebook']==1){
			       $dt['avatar1']="https://graph.facebook.com/".$dt['facebook_id']."/picture";
				   $dt['lavatar1']="https://graph.facebook.com/".$dt['facebook_id']."/picture?type=large";
           
			}else{
				$dt['avatar1']=AVATAR_DEFAULT;
			}
			
			$db2=new db;
			$db2->connect();
			 //GET BLOG,VIDEO,TRUYEN ------
			 		//blog
			 $sql="select count(*) as post from index_post where username='".$u."' and is_text=1";
			 $db2->query($sql);
			 $num=$db2->fetchOne();
			$dt['num']['blog']=$num['post'];
						//video
			 $sql="select count(*) as post from index_post where username='".$u."' and is_video=1";
			 $db2->query($sql);
			 $num=$db2->fetchOne();
			 $dt['num']['video']=$num['post'];
			 	//truyen
			 $sql="select count(*) as post from truyen where user_upload='".$u."'";
			 $db2->query($sql);
			 $num=$db2->fetchOne();
			 $dt['num']['truyen']=$num['post'];
			 // Xong, close db connection
			 $db2->close();
			
			$c->save($u,$dt,86000,"user");
			return $dt;
		}else{
			return "";
		}
	}
	return $dt;
	

}

function get_post($id){
		$db=new db;
		$db->connect();
		$db->query("select * from index_post where id=".$id);
		$dt=$db->fetchOne();
		if($dt['is_image']==1){
			$tb='post_image';
			}
			if($dt['is_video']==1){
			$tb='post_media';
			}
			if($dt['is_text']==1){
			$tb='post_text';
			}
			
			return cache_get_post($dt['post_id'],$tb,$dt);
	}

function cache_get_post($id,$table="post_image",$data=array()){
	
	$c=new cache;
	$dt=$c->load($data['id'],"post/".$table);
	
	
	if(!$dt){
	
		 $sql="select n.*,c.name as cat_name,c.id as cat_id,c.alias as cat_alias,s.name as sub_name,s.id as sub_id,s.alias as sub_alias from ".$table." as n 
		 left join category as c
			 on c.id=n.cat_id
		 left join category as s
		 	on s.id=n.sub_id
		 where n.id=".$id;
		 $db=new db;
		 $db->connect();
		 $db->query($sql);
		 
		 if($db->num_rows()>0){
			 $dt=$db->fetchOne();
			 
			 //GET STAT INFOR FROM INDEX POST NHU VIEW, PID...(like,comments,...)
			 $dt['pid']=$data['id'];
			 $dt['views']=$data['views'];
			 if($table=='post_text'){
					 $dt['category']="<a href='".url_category($dt['cat_id'],$dt['cat_alias'],'blog')."' title='".$dt['cat_name']."' >".$dt['cat_name']."</a>";
					 if($dt['sub_id']!=''){
					  $dt['subcategory']="<a href='".url_subcategory($dt['sub_id'],$dt['sub_alias'],'blog')."' title='".$dt['sub_name']."' >".$dt['sub_name']."</a>";
					 }
			 }else
			 if($table=='post_media'){
				  $dt['category']="<a href='".url_category($dt['cat_id'],$dt['cat_alias'],'video')."' title='".$dt['cat_name']."' >".$dt['cat_name']."</a>";
					 if($dt['sub_id']!=''){
					  $dt['subcategory']="<a href='".url_subcategory($dt['sub_id'],$dt['sub_alias'],'video')."' title='".$dt['sub_name']."' >".$dt['sub_name']."</a>";
					 }
				 
			 }
			 
			 //FACEBOOK QUERY GET STATIC -----------------
			
			// $fb=get_facebook_info($url);
			// $db->nonquery('update index_post set comments='.$fb['comment'].',likes='.$fb['like'].' where id='.$data['id']);	
			 
			
			 //End of facebok update sql query-----		 
			 
			 
			 switch($table){
			 case "post_image":
			
			 	$dt['type']='image';
			 	$im = explode('_', $dt['image']);
				$im2=$im[0]."/".$im[1]."/".$im[2]."/".$dt['image'];
				
				$gifext=strtolower(substr($dt['image'],-3,3));
				if($gifext=='gif'){
					$dt['image_gif']=1;
					$image_big="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
					$image_med="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
					$image_small="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
				}else{
					$dt['image_gif']=0;
					$image_big="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_BIG."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
					$image_med="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_MED."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
					$image_small="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_SMALL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
				}
				
				$dt['image'] =IMAGE_URL.IMAGE_PICTURE.IMAGE_BIG."/".$im2;
				$dt['image_big'] = IMAGE_URL.IMAGE_PICTURE.IMAGE_BIG."/".$im2;
				$dt['image_near_big'] = IMAGE_URL.IMAGE_PICTURE.IMAGE_NEAR_BIG."/".$im2;
				$dt['image_med'] =IMAGE_URL.IMAGE_PICTURE.IMAGE_MED."/".$im2;
				$dt['image_small'] =IMAGE_URL.IMAGE_PICTURE.IMAGE_SMALL."/".$im2;
			
			
				 //$image_val="<img src='".$dt['image']."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
				//GET SOURCE IMAGE
						$db->query("select * from  images where post_id=".$dt['id']);
						if($db->num_rows()>0){
								$link=$db->fetchAll();

								foreach($link as $val){
								$im = explode('_', $val['image']);
								$im2=$im[0]."/".$im[1]."/".$im[2]."/".$val['image'];
								
								$gifext=strtolower(substr($val['image'],-3,3));
									if($gifext=='gif'){
		$image_big.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
		$image_near_big.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
		$image_med.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
		$image_small.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_ORIGINAL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
									}else{
	$image_big.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_BIG."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
	$image_near_big.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_NEAR_BIG."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
	$image_med.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_MED."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
	$image_small.="<img src='".IMAGE_URL.IMAGE_PICTURE.IMAGE_SMALL."/".$im2."' class='img-rounded img-responsive' style='margin:auto' /><br/>";
									}
								}
							}
							
				$dt['images_big'] = $image_big;
				$dt['images_near_big'] = $image_near_big;
				$dt['images_small'] = $image_small;
				$dt['images_med'] = $image_med;
				
				break;
			 case "post_text":
				$dt['type']='text';
				if($dt['image']!=''){
			 	$im = explode('_', $dt['image']);
				$im2=$im[0]."/".$im[1]."/".$im[2]."/".$dt['image'];
				$dt['image'] =IMAGE_URL.IMAGE_TEXT.IMAGE_NEAR_BIG."/".$im2; //SIZE 400x200
				$dt['image_big'] = IMAGE_URL.IMAGE_TEXT.IMAGE_BIG."/".$im2; //Size 500px
				$dt['image_near_big'] = IMAGE_URL.IMAGE_TEXT.IMAGE_NEAR_BIG."/".$im2;
				$dt['image_med'] =IMAGE_URL.IMAGE_TEXT.IMAGE_MED."/".$im2;
				$dt['image_small'] =IMAGE_URL.IMAGE_TEXT.IMAGE_SMALL."/".$im2;
				$dt['description']=blog_image_lazy($dt['description'],$dt['alias']);
				$dt['description']= BBCode::parse($dt['description']); 
				}
				
				break;	
			  case "post_media":
			    $dt['type']='video';
			  try{
			 	$u=videofactory::create($dt['media_type']);
				$u->setLink($dt['url']);
				$param=$u->getQueryParams();
				if($param['list']){
					$u->setEmbedCode($param, true);
				}else{
					$u->setEmbedCode($param);
				}
				$dt['embed'] = $u->getEmbedCode();
				$dt['image'] ="http://img.youtube.com/vi/".$param['v']."/0.jpg";
				$dt['image_big'] ="http://img.youtube.com/vi/".$param['v']."/0.jpg";
				$dt['image_med'] ="http://img.youtube.com/vi/".$param['v']."/0.jpg";
				$dt['image_near_big'] ="http://img.youtube.com/vi/".$param['v']."/0.jpg";
				$dt['image_small'] ="http://img.youtube.com/vi/".$param['v']."/2.jpg";
				
				}catch (Exception $e){
				
				}
				
				break;
			  
			 }
				
			 
		
			
			 $dt['date_created'] = time_ago($data['date_created']);
			 
			 $db->close();
			 $c->save($data['id'],$dt,86000,"post/".$table);
			 return $dt;
		 }else{
		  	$db->close();
			return false;
		 }
	}
	
	//Update new thong so moi
	 $dt['pid']=$data['id'];
	 $dt['views']=k_format($data['views']);
	 $dt['date_created'] = time_ago($data['date_created']);
	
	return $dt;

}


function cache_get_album($id){
	
	$c=new cache;
	$dt=$c->load($id,"album");
	if(!$dt){
		 $sql="select * from album where id=".$id;
		 $db=new db;
		 $db->connect();
		 $db->query($sql);
		 
		 if($db->num_rows()>0){
			 $dt=$db->fetchOne();
			 
			 //GET POST,VIEW , LIKE , COMMENT ------
			 $db->query("select sum(views) as views,sum(likes) as likes,sum(comments) as comments,count(*) as post from index_post where id_album=".$id);
			 $num=$db->fetchOne();
			 $dt['image']=album_get_image($id);
			 $dt['comments']=$num['comments'];
			 $dt['likes']=$num['likes'];
			 $dt['views']=$num['views'];
			 $dt['posts']=$num['post'];
			
			 $dt['list_item']=album_get_list_item($dt['id']);
			 $db->nonquery("update album set comments=".$num['comments'].",likes=".$num['likes'].",views=".$num['views']." where id=".$id);
			 
			 // GET LIST ITEM IN THIS ALBUM -------
			 
			 //-----------
			 $db->close();
			 $c->save($id,$dt,300000,"album");
			 return $dt;
		 }else{
		  	$db->close();
			return false;
		 }
	}
	return $dt;

}



function album_get_image($id){
	$db=new db;
	$db->connect();
	$db->query('select id from index_post where id_album='.$id.' order by id asc limit 0,1');
	if($db->num_rows()>0){
		$row=$db->fetchOne();
		$rdt=get_post($row['id']);
		return $rdt['image'];
	}
	return IMAGE_DEFAULT_ALBUM;
}


function album_get_list_item($id){
    $db=new db;
	$db->connect();
	$db->query('select id from index_post where album_id='.$id.' order by id desc limit 0,5');
	if($db->num_rows()>0){
		$dt=$db->fetchAll();
		$data= array();
		foreach($dt as $row){
			$post=get_post($row['id']);
			$post2['username']=$post['username'];
			$post2['views']=$post['views'];
			$post2['image']=$post['image'];
			$post2['image_small']=$post['image_small'];
			$post2['title']=$post['title'];
			$post2['pid']=$row['id'];
			$data[]=$post2;
		}
		return $data;
	}else{
	return false;	
		}
}



function cache_get_category($id){
	$cache=new cache;
	$dt=$cache->load('category'.$id,'category');
	if(!$dt){
		$db=new db;
		$db->connect();
		$db->query('select * from category where id='.$id);
		$dt=$db->fetchOne();
		$cache->save('category'.$id,$dt,3600,'category');
	}
	return $dt;
	
	}

function cache_get_album_num_user($username){
	$db=new db;
	$db->connect();
	$db->query("select count(*) as num from album where username='".$username."'");
	$dt=$db->fetchOne();
	$db->close();
	return $dt['num'];
}
	
//-------- Video function -----
function get_list_category_video(){
	$cache=new cache;
	$str=$cache->load('list_video_category','category');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from category where video_type=1 order by p_id asc,thutu asc');
	 $dt=$db->fetchAll();
	 $data=array();
	 	foreach($dt as $row){
			if($row['p_id']==0){
				$row['url']=url_category($row['id'],$row['alias'],'video');
			}else{
				$row['url']=url_subcategory($row['id'],$row['alias'],'video');
			}
			$data[]=$row;
		}
	/*	
	$str= "<ul class='box_theloai_ul'>";
	foreach($data as $row){
		$str.=" <li><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></li>";
	}
	$str.= "</ul>";
	
	*/
	$color=array('#c0392b','#e84c3d','#e67e22','#f1c40f','#2ecc71','#27ae60','#1abc9c','#16a085','#3498db','#2980b9','#9b59b6');
	$str="<table class='table  table_blog_theloai'>";
	$j=1;
	foreach($data as $row){
		if($j%2==1){
			$str.="<tr><td style='background-color:".$color[$j-1]."'><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></td>";
		}
		if($j%2==0){
			$str.="<td style='background-color:".$color[$j-1]."'><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></td></tr>";
		}
		$j++;
	}
	if(($j-1)%2==1){
		$str.="<td>&nbsp;</td></tr>";
	}
	
	
	$str.="</table>";
		
	$cache->save('list_video_category',$str,3600,'category');	
	}
	return $str;
	
}

function get_list_category_video_mobi(){
	$cache=new cache;
	$str=$cache->load('list_video_category_mobi','category');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from category where video_type=1 order by p_id asc,thutu asc');
	 $dt=$db->fetchAll();
	 $data=array();
	 	foreach($dt as $row){
			if($row['p_id']==0){
				$row['url']=url_category($row['id'],$row['alias'],'video');
			}else{
				$row['url']=url_subcategory($row['id'],$row['alias'],'video');
			}
			$data[]=$row;
		}

	$str.="<li><a href='".HOME."video' ><span aria-hidden='true' class='icon-home'></span>Video</a></li>";
	foreach($data as $row){
				$str.="<li><a href='".$row['url']."' title='".$row['name']."'><span aria-hidden='true' class='icon-minus'></span>".$row['name']."</a></li>";
	
	}
	
		
	$cache->save('list_video_category_mobi',$str,3600,'category');	
	}
	return $str;
	
}

function cache_get_video_comment(){
	$cache=new cache;
	$str=$cache->load('list_video_comment','video');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select * from comment where type='video' order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['type_id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if(0){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['tieude']."  <span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div>
						<div class='sdes'>".$row['noidung']."</div>
						</a>"; 
                    }else{ // End $j==1
                              $str.="<a title='".$row['tieude']."' href='".$row['url']."' class='list-group-item'><span class='tieude'>".cutstring($row['tieude'], 40)."</span><span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div><div class='sdes'>".cutstring($row['noidung'], 100)."</div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_video_comment',$str,600,'video');
	}
	
	return $str;

} // End get video comment -----



function cache_get_video_view(){
	$cache=new cache;
	$str=$cache->load('list_video_view','video');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_video=1 order by views desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['description']=$item['description'];
				$row['url']=url_post($row['id'],$item['alias'],'video');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						
						</a>"; 
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_video_view',$str,600,'video');
	}
	
	return $str;

} // End get video view -----


function cache_get_video_view_day(){
	$cache=new cache;
	$str=$cache->load('list_video_view_day','video');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_video=1 and last_update_view='".date('Y-m-d')."' order by view_day desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['description']=$item['description'];
				$row['url']=url_post($row['id'],$item['alias'],'video');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						
						</a>"; 
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_video_view_day',$str,600,'video');
	}
	
	return $str;

} // End get video view day -----


function cache_get_video_view_week(){
	$cache=new cache;
	$str=$cache->load('list_video_view_week','video');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_video=1 and WEEK(last_update_view, 3) = '" . date('W') . "' order by view_week desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['description']=$item['description'];
				$row['url']=url_post($row['id'],$item['alias'],'video');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						
						</a>"; 
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_video_view_week',$str,600,'video');
	}
	
	return $str;

} // End get video view week -----


function cache_get_video_pick(){
	$cache=new cache;
	$str=$cache->load('list_video_pick','video');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_video=1 and pick=1 order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['image_small']=$item['image_small'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['description']=$item['description'];
				$row['url']=url_post($row['id'],$item['alias'],'video');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
               	if($j==1){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']."  <br /><span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						<div class='sdes'>".$row['description']."</div>
						</a>"; 
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_video_pick',$str,600,'video');
	}
	
	return $str;

} // End get video pick -----

//End video function ----------

//---------Blog function -------

function get_list_category_blog(){
	$cache=new cache;
	$str=$cache->load('list_blog_category','blog');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from category where text_type=1 order by p_id asc,thutu asc');
	 $dt=$db->fetchAll();
	 $data=array();
	 	foreach($dt as $row){
			if($row['p_id']==0){
				$row['url']=url_category($row['id'],$row['alias'],'blog');
			}else{
				$row['url']=url_subcategory($row['id'],$row['alias'],'blog');
			}
			$data[]=$row;
		}
	/*	
	$str= "<ul class='box_theloai_ul'>";
	foreach($data as $row){
		$str.=" <li><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></li>";
	}
	$str.= "</ul>";
		*/
		$color=array('#c0392b','#e84c3d','#e67e22','#f1c40f','#2ecc71','#27ae60','#1abc9c','#16a085','#3498db','#2980b9','#9b59b6','#2c3e50','#e67e22','#e74c3c','#f39c12');
		$str="<table class='table table_blog_theloai'>";
	$j=1;
	foreach($data as $row){
		if($j%2==1){
			$str.="<tr><td style='background-color:".$color[$j-1]."'><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></td>";
		}
		if($j%2==0){
			$str.="<td style='background-color:".$color[$j-1]."'><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['name']."'>".$row['name']."</a></td></tr>";
		}
		$j++;
	}

	if(($j-1)%2==1){
		$str.="<td>&nbsp;</td></tr>";
	}
	
	
	$str.="</table>";
	
	$cache->save('list_blog_category',$str,3600,'blog');	
	}
	
	return $str;
	
}

function get_list_category_blog_mobi(){
	$cache=new cache;
	$str=$cache->load('list_blog_category_mobi','blog');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from category where text_type=1 order by p_id asc,thutu asc');
	 $dt=$db->fetchAll();
	 $data=array();
	 	foreach($dt as $row){
			if($row['p_id']==0){
				$row['url']=url_category($row['id'],$row['alias'],'blog');
			}else{
				$row['url']=url_subcategory($row['id'],$row['alias'],'blog');
			}
			$data[]=$row;
		}
	$str.="<li><a href='".HOME."blog' ><span aria-hidden='true' class='icon-home'></span>Blog</a></li>";
	foreach($data as $row){
				$str.="<li><a href='".$row['url']."' title='".$row['name']."'><span aria-hidden='true' class='icon-minus'></span>".$row['name']."</a></li>";
		}

	$cache->save('list_blog_category_mobi',$str,3600,'blog');	
	}
	
	return $str;
	
}

function cache_get_blog_comment(){
	$cache=new cache;
	$str=$cache->load('list_blog_comment','blog');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select * from comment where type='blog' order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['type_id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if(0){
						if($row['image']!=''){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['tieude']."  <span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div>
						</a>"; 
						}else{
						$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'>".$row['tieude']."  <span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div>
						</a>";
						}
                    }else{ // End $j==1
                              $str.="<a title='".$row['tieude']."' href='".$row['url']."' class='list-group-item'><span class='tieude'>".cutstring($row['tieude'], 40)."</span><span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div><div class='sdes'>".cutstring($row['noidung'], 100)."</div> </a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_blog_comment',$str,600,'blog');
	}
	
	return $str;

} // End get blog comment -----



function cache_get_blog_view(){
	$cache=new cache;
	$str=$cache->load('list_blog_view','blog');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_text=1 order by views desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['sdes']=$item['sdes'];
				$row['url']=url_post($row['id'],$item['alias'],'blog');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
						if($row['image']!=''){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}else{
						$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_blog_view',$str,600,'blog');
	}
	
	return $str;

} // End get blog view -----


function cache_get_blog_view_day(){
	$cache=new cache;
	$str=$cache->load('list_blog_view_day','blog');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_text=1 and last_update_view = '" .date('Y-m-d'). "' order by view_day desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['sdes']=$item['sdes'];
				$row['url']=url_post($row['id'],$item['alias'],'blog');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
						if($row['image']!=''){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}else{
						$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_blog_view_day',$str,600,'blog');
	}
	
	return $str;

} // End get blog view day -----


function cache_get_blog_view_week(){
	$cache=new cache;
	$str=$cache->load('list_blog_view_week','blog');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_text=1 and WEEK(last_update_view, 3) = '" . date('W') . "' order by view_week desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['sdes']=$item['sdes'];
				$row['url']=url_post($row['id'],$item['alias'],'blog');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
						if($row['image']!=''){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}else{
						$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_blog_view_week',$str,600,'blog');
	}
	
	return $str;

} // End get blog view week -----


function cache_get_blog_pick(){
	$cache=new cache;
	$str=$cache->load('list_blog_pick','blog');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select id from index_post  where is_text=1 and pick=1 order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			$dt=array();
			foreach($data as $row){
				$item=get_post($row['id']);
				$row['image']=$item['image'];
				$row['views']=$item['views'];
				$row['title']=$item['title'];
				$row['sdes']=$item['sdes'];
				$row['url']=url_post($row['id'],$item['alias'],'blog');
				$dt[]=$row;
			}
			
			
              $str="<div class='list-group'>";
              $j=1;
               foreach($dt as $row){
                 	if($j==1){
                 	if($row['image']!=''){
               			$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'> <img src='".$row['image']."' class='img-responsive'/>".$row['title']." <br /> <span class='view'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						</a>"; 
						}else{
						$str.=" <a href='".$row['url']."' class='list-group-item top_video_item'>".$row['title']."  <span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div>
						<div class='sdes'>".$row['sdes']."</div></a>"; 
						}
                    }else{ // End $j==1
                              $str.="<a href='".$row['url']."' class='list-group-item'>".$row['title']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_blog_pick',$str,600,'blog');
	}
	
	return $str;

} // End get blog pick -----


function cache_get_related_post($cat,$sub){
	if($sub){
		$cache=new cache;
		$data=$cache->load('related_blog_sub'.$sub,'blog');
		if(!$data){
			$db=new db;
			$db->connect();
			$db->query("select id,title from index_post where sub_id=".$sub." order by id desc limit 0,6");
			if($db->num_rows()>0){
			$data=$db->fetchAll();
			$cache->save('related_blog_sub'.$sub,$data,1800,'blog');
			}
		}
	}else
	if($cat){
		$cache=new cache;
		$data=$cache->load('related_blog_cat'.$cat,'blog');
		if(!$data){
			$db=new db;
			$db->connect();
			$db->query("select id,title from index_post where cat_id=".$cat." order by id desc limit 0,6");
			if($db->num_rows()>0){
			$data=$db->fetchAll();
			$cache->save('related_blog_cat'.$cat,$data,1800,'blog');
			}
		}
	}
	return $data;
}

// End blog function

//--------Truyen function
function cache_get_truyen($id){
	$cache=new cache;
	$data=$cache->load($id,'truyen/'.$id);
	if(!$data){
		$db=new db;
		$db->connect();
		$db->query('select truyen.* from truyen
				 	where truyen.id='.intval($id));
		$data=$db->fetchOne();
		if($data['tinhtrang']==1){
			$data['tinhtrang-text']="<a href='".HOME."truyen/status/truyen-hoan-thanh.html'>Hoàn Thành</a>";
		}else
		if($data['tinhtrang']==2){
			$data['tinhtrang-text']= "<a href='".HOME."truyen/status/truyen-dang-cap-nhat.html'>Đang Tiến Hành</a>";
		}else{
			$data['tinhtrang-text']= "<a href='".HOME."truyen/status/truyen-tam-ngung.html'>Tạm Ngưng</a>";
		}
			$data['tag']=truyen_get_tag($data['tags']);
		
		//Get thể loại
	
		//------ End of get theloai
		//get facebook static để update, like, comment
		//$fb=new shareCount(url_truyen($data['id'],$data['alias']));
		//$count=$fb->get_fb();
		
		// Tim link chuong moi nhat, userupdate,time update,tongsochuong hien tai
		$db->query('select id,ten,alias,time_upload,user_upload from truyen_chuong where truyen_id='.$id.' order by thutu desc limit 0,1');
		if($db->num_rows()>0){
			$dt=$db->fetchOne();
			$c_url=url_truyen_chuong($dt['id'],$dt['alias'],$data['alias']);
			$db->nonquery('update truyen set chuongmoinhat="'.$dt['ten'].'",chuongmoinhat_url="'.$c_url.'",time_update="'.$dt['time_upload'].'",user_update="'.$dt['user_upload'].'" where id='.$id);
		}
		
		
		$data['chuongmoinhat']=$dt['ten'];
		$data['chuongmoinhat_url']=$c_url;
		$data['user_update']=$dt['user_upload'];
		$data['time_update']=$dt['time_upload'];
	
		//Tìm tổng số chương
		 $db->query(" select count(*) as num from truyen_chuong where truyen_id=".$id);
		 $dt2=$db->fetchOne();
		 $data['sochuong']=$dt2['num'];
		 
		 //Tim chuong bat dau
		 $db->query('select id,ten,alias from truyen_chuong where truyen_id='.$id.' order by thutu asc limit 0,1');
		 $dt=$db->fetchOne();
		 $data['chuongbatdau']=$dt['ten'];
		 $data['chuongbatdau_url']=url_truyen_chuong($dt['id'],$dt['alias'],$data['alias']);
		 
		
		 // Update table the loai cai coi
		 $sql="update truyen_theloai_fk set views=".$data['views'].",likes=".$data['likes'].",comments=".$data['comments'].",time_upload='".$data['time_upload']."',tinhtrang=".$data['tinhtrang'].",time_update='".$data['time_update']."',tongsochuong=".$data['sochuong'].",view_week=".$data['view_week'].",view_month=".$data['view_month']." where truyen_id=".$id ;
		// echo $sql;
		$db->nonquery($sql);
		
		$gtags=gtags($data['ten']);
		if($data['gtags']==0){
		
			gtags_add($gtags,$data['id']);
			
			
		}
	
		
		$data['gtags']=gtags_link($gtags);
	
		 	
		 $db->close();
		$data['theloai2']=get_truyen_theloai($data['theloai']);
		
		
		$cache->save($id,$data,86000,'truyen/'.$id);
	}
	return $data;
}
function cache_get_truyen_chuong_list($id,$page){
	$start=($page-1)*30;
	$cache=new cache;
	$data=$cache->load($page,'truyen/'.$id.'/chuong_list');
	if(!$data){
		$db=new db;
		$db->connect();
		$db->query('select id,ten,alias,thutu,views,time_upload,user_upload from truyen_chuong where trangthai=1 and xoa=0 and truyen_id='.$id.' order by thutu desc limit '.$start.',30');
		$data=$db->fetchAll();
		
		$cache->save($page,$data,86000,'truyen/'.$id.'/chuong_list');
		$db->close();
		
		}
	return $data;
}

function cache_get_truyen_chuong($id){

	//$cache=new cache;
	//$data=$cache->load($id,'truyen/chuong');
	if(!$data){
		$table=table_chuong($id);
		$db=new db;
		$db->connect();
		$db->query('select * from truyen_chuong where id='.intval($id));
		$data=$db->fetchOne();
		$db->query('select * from '.$table.' where truyen_chuong_id='.intval($id));
		$data2=$db->fetchOne();
		$data['noidung']=$data2['noidung'];
		
		
		// GET LINK NEXT AND PREVIOUS
		$db->query('select id,ten,alias from truyen_chuong where truyen_id='.$data['truyen_id'].' and thutu <'.$data['thutu']." order by thutu desc limit 0,1");
		if($db->num_rows()>0){
		$data['previous']=$db->fetchOne();
		
		}else{
		$data['previous']='';	
		}
		
		$db->query('select id,ten,alias from truyen_chuong where truyen_id='.$data['truyen_id'].' and thutu >'.$data['thutu']." order by thutu asc limit 0,1");
		if($db->num_rows()>0){
		$data['next']=$db->fetchOne();
		}else{
		$data['next']='';	
		}
		//
		
	//	$cache->save($id,$data,8600,'truyen/chuong');
		$db->close();
		
		}
	return $data;
}


function get_list_theloai_truyen(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category','truyen');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from truyen_theloai  order by thutu asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 	foreach($dt as $row){
			
				$row['url']=url_truyen_theloai($row['id'],$row['alias']);
			
			$data[]=$row;
		}
		$row['url']=HOME."truyen/status/truyen-hoan-thanh.html";
		$row['ten']="Truyện Full";
		$data[]=$row;
		$row['url']=HOME."truyen/status/truyen-moi.html";
		$row['ten']="Truyện Mới";
		$data[]=$row;

		$row['url']=HOME."truyen/status/truyen-hay.html";
		$row['ten']="Truyện Hay";
		$data[]=$row;
		$row['url']=HOME."truyen";
		$row['ten']="Mới Cập Nhật";
			
		$data[]=$row;
		
	
	$str="<table class='table  table_truyen_theloai'>";
	$j=1;
	$i=1;
	$color=array('#c0392b','#e84c3d','#e67e22','#f1c40f','#2ecc71','#27ae60','#1abc9c','#16a085','#3498db','#2980b9','#c0392b','#e84c3d');
	foreach($data as $row){
		if($j%2==1){
			$str.="<tr style='background-color:".$color[$i]."'><td><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></td>";
			$i++;
		}
		if($j%2==0){
			$str.="<td><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></td></tr>";
		}
		$j++;
	}

	if(($j-1)%2==1){
		$str.="<td>&nbsp;</td></tr>";
	}
	
	
	$str.="</table>";
	
		
	$cache->save('list_truyen_category',$str,3600,'truyen');	
	}
	return $str;
	
}


function get_list_theloai_truyen_mobi(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category_mobi','truyen');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from truyen_theloai  order by ten asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 $row['url']=HOME."truyen/status/truyen-hoan-thanh.html";
		$row['ten']="Truyện Full";
		$data[]=$row;
		$row['url']=HOME."truyen/status/truyen-moi.html";
		$row['ten']="Truyện Mới ";
		$data[]=$row;
		$row['url']=HOME."truyen";
		$row['ten']="Mới Cập Nhật";
		$data[]=$row;
		$row['url']=HOME."truyen/status/truyen-hay.html";
		$row['ten']="Truyện Hay";
		$data[]=$row;
	
	 	foreach($dt as $row){
			$row['url']=url_truyen_theloai($row['id'],$row['alias']);
			$data[]=$row;
		}
		$str.="<li><a href='".HOME."truyen' title='".$row['ten']."'><span aria-hidden='true' class='icon-home'></span>Kho Truyện</a></li>";
		foreach($data as $row){
			$str.="<li><a href='".$row['url']."' title='".$row['ten']."'><span aria-hidden='true' class='icon-minus'></span>".$row['ten']."</a></li>";
	 }
	$cache->save('list_truyen_category_mobi',$str,3600,'truyen');	
	}
	return $str;
	
}
function get_list_theloai_truyen_mobi2(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category_mobi2','truyen');
	if(!$str){
	 $db=new db;
	 $db->connect();
	 $db->query('select * from truyen_theloai  order by ten asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 	foreach($dt as $row){
			$row['url']=url_truyen_theloai($row['id'],$row['alias']);
			$data[]=$row;
		}
		foreach($data as $row){
			$str.="<li><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></li>";
	 }
	$cache->save('list_truyen_category_mobi2',$str,3600,'truyen');	
	}
	return $str;
	
}

function get_truyen_theloai($str){
		
		$theloaiar=explode(",",$str);
		$str='';
		$str2='';
			$db2=new db;
			$db2->connect();
			foreach($theloaiar as $value ){
				$value=intval($value);
				$db2->query('select alias,ten from truyen_theloai where id='.$value);
				$dt=$db2->fetchOne();
				$str.="<a href='".url_truyen_theloai($value,$dt['alias'])."'> <i class='fa fa-tags'></i> ".$dt['ten']." </a> ";
				$str2.="<a href='".url_truyen_theloai($value,$dt['alias'])."'  itemprop='url'> <span itemprop='title'>".$dt['ten']."</span></a> ,";
										
			}
			$str=substr($str,0,strlen($str)-1);
			$str2=substr($str2,0,strlen($str2)-1);
		$db2->close();
		$row['1']=$str;
		$row['2']=$str2;
		return $row;
}

function get_truyen_view(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view','truyen');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query('select id,ten,alias,views,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen order by views desc limit 0,10');
		
		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
				$j=1;
			foreach($dt as $row){
							if($j==1){
								$str.=" <a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'> ".$row['chuongmoinhat']."</p></a>";  
							}else{
								 $str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>"; 
							}
							$j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view',$str,1800,'truyen');
	} // End !$data
	
	return $str;

}// End function get truyen view

function get_truyen_view_day(){

	$cache=new cache;
	$str=$cache->load('get_truyen_view_day','truyen');

	if(!$str){

		$db=new db;
		$db->connect();
		$db->query("select id,ten,alias,view_day,chuongmoinhat,chuongmoinhat_url,hinhanh,tinhtrang from truyen where last_update_view='".date('Y-m-d')."' order by view_day desc limit 0,10");

		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
				if($j==1){
					$str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item top_truyen_bg'>
									<p class='box-news-image'><img width='60' height='94' src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>";
				}else{

					$str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item'><span class='medal'>".$j."</span>".$row['ten']."<span class='view pull-right'>".$row['view_day']." Lượt xem</span></a>";
				}
				$j++;

			} // End foreach
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_day',$str,1800,'truyen');
	} // End !$data

	return $str;

} // End of function get truyen view day


function get_truyen_view_day1(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view_day','truyen');
	
	if(!$str){
		
		$db=new db;
		$db->connect();
		$db->query("select id,ten,alias,view_day,chuongmoinhat,chuongmoinhat_url,hinhanh,tinhtrang from truyen where last_update_view='".date('Y-m-d')."' order by view_day desc limit 0,10");
		
	$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
					if($j==1){
									$str.=" <a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>"; 
							}else{
								 $str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['view_day']." Lượt xem</span></a>"; 
							}
							$j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_day',$str,1800,'truyen');
	} // End !$data
	
	return $str;

} // End of function get truyen view day

function get_truyen_view_week(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view_week','truyen');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,ten,alias,view_week,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen where WEEK(last_update_view, 3) = '" . date('W') . "' order by view_week desc limit 0,10");
		
	$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
				if($j==1){
									$str.=" <a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>"; 
							}else{
								 $str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['view_week']." Lượt xem</span><div class='clearfix'></div></a>"; 
					}
								 $j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_week',$str,1800,'truyen');
	} // End !$data
	
	return $str;

} // End of function get truyen view week


function get_truyen_view_month(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view_month','truyen');
	if(!$str){
		$db=new db;
		$db->connect();
		$db->query("select id,ten,alias,view_month,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen where MONTH(last_update_view) = '" . date('m') . "' order by view_month desc limit 0,10");
		
		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
					if($j==1){
									$str.=" <a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>"; 
							}else{
								 $str.="<a href='".url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['view_month']." Lượt xem</span><div class='clearfix'></div></a>"; 
							}
							$j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_month',$str,1800,'truyen');
	} // End !$data
	
	return $str;

} // End of function get truyen view month

function truyen_clear_cache_chuong($truyen,$chuong,$thutu){
	//Clear cache truyen -----
	$cache=new cache;
	$cache->flush($truyen,'truyen/'.$truyen);
	$db=new db;
	$db->connect();
	$db->query('select id from truyen_chuong where truyen_id='.$truyen.' and thutu <'.$thutu.' order by thutu desc limit 0,1');
	if($db->num_rows()>0){
		$row=$db->fetchOne();
		$cache->flush($row['id'],'truyen/chuong/'.$row['id']);
	}
	$db->close();
	//Clear page dau tien cua cache de cap nhat chuong cuoi
	if(is_dir(CACHE_PATH.'/truyen/'.$truyen.'/chuong_list')){
	remove_dir(CACHE_PATH.'/truyen/'.$truyen.'/chuong_list');
	}
}


function cache_get_truyen_comment(){
	$cache=new cache;
	$str=$cache->load('list_truyen_comment','truyen');
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select * from comment where type='truyen' order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
			 
			
			
              $str="<div class='list-group' id='right-comment-list'>";
              $j=1;
               foreach($data as $row){
                 	if(0){
               			
                    }else{ // End $j==1
                              $str.="<a title='".$row['tieude']."' href='".$row['url']."' class='list-group-item'><span class='tieude'>".cutstring($row['tieude'], 40)."</span><span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div><div class='sdes'>".cutstring($row['noidung'], 100)."</div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_truyen_comment',$str,600,'truyen');
	}
	
	return $str;

} //---------End truyen function -------
